<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/blog/images/logo.svg" color="#222">

<link rel="stylesheet" href="/blog/css/main.css">


<link rel="stylesheet" href="/blog/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"dttx123.gitee.io","root":"/blog/","scheme":"Gemini","version":"8.0.0-rc.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="注册事件的两种方式on+事件名称 onclick、onmouseover这种on+事件名称的方式注册事件几乎所有的浏览器都支持。  注册事件： 123box.onclick &#x3D; function()&amp;#123;	&#x2F;&#x2F;事件处理程序	&amp;#125;  移除事件： 1box.onclick &#x3D; null;  on+事件名称注册事件的缺点： 同一个元素同一类型的事件，只能注册一个，如果注册了多个，会出现覆盖">
<meta property="og:type" content="article">
<meta property="og:title" content="webAPI04">
<meta property="og:url" content="http://dttx123.gitee.io/2016/08/07/webAPI/04/index.html">
<meta property="og:site_name" content="Starry Night">
<meta property="og:description" content="注册事件的两种方式on+事件名称 onclick、onmouseover这种on+事件名称的方式注册事件几乎所有的浏览器都支持。  注册事件： 123box.onclick &#x3D; function()&amp;#123;	&#x2F;&#x2F;事件处理程序	&amp;#125;  移除事件： 1box.onclick &#x3D; null;  on+事件名称注册事件的缺点： 同一个元素同一类型的事件，只能注册一个，如果注册了多个，会出现覆盖">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://dttx123.gitee.io/blog/2016/08/07/webAPI/04/keyCode.jpg">
<meta property="og:image" content="http://dttx123.gitee.io/blog/2016/08/07/webAPI/04/1.png">
<meta property="og:image" content="http://dttx123.gitee.io/blog/2016/08/07/webAPI/04/bubble.png">
<meta property="og:image" content="http://dttx123.gitee.io/blog/2016/08/07/webAPI/04/capture.png">
<meta property="og:image" content="http://dttx123.gitee.io/blog/2016/08/07/webAPI/04/three.png">
<meta property="article:published_time" content="2016-08-06T16:00:00.000Z">
<meta property="article:modified_time" content="2020-05-13T03:06:59.456Z">
<meta property="article:author" content="大春哥">
<meta property="article:tag" content="dom操作">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://dttx123.gitee.io/blog/2016/08/07/webAPI/04/keyCode.jpg">

<link rel="canonical" href="http://dttx123.gitee.io/2016/08/07/webAPI/04/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>webAPI04 | Starry Night</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
        <span class="toggle-line toggle-line-first"></span>
        <span class="toggle-line toggle-line-middle"></span>
        <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/blog/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">Starry Night</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">这就朕为你打下的江山</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/blog/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/blog/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/blog/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/blog/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/blog/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://dttx123.gitee.io/2016/08/07/webAPI/04/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/blog/images/avatar.jpg">
      <meta itemprop="name" content="大春哥">
      <meta itemprop="description" content="IT">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Starry Night">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          webAPI04
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2016-08-07 00:00:00" itemprop="dateCreated datePublished" datetime="2016-08-07T00:00:00+08:00">2016-08-07</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-05-13 11:06:59" itemprop="dateModified" datetime="2020-05-13T11:06:59+08:00">2020-05-13</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/dom%E6%93%8D%E4%BD%9C/" itemprop="url" rel="index"><span itemprop="name">dom操作</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="注册事件的两种方式"><a href="#注册事件的两种方式" class="headerlink" title="注册事件的两种方式"></a>注册事件的两种方式</h1><h2 id="on-事件名称"><a href="#on-事件名称" class="headerlink" title="on+事件名称"></a>on+事件名称</h2><blockquote>
<p>onclick、onmouseover这种on+事件名称的方式注册事件几乎所有的浏览器都支持。</p>
</blockquote>
<p>注册事件：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">box.onclick = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">	<span class="comment">//事件处理程序	</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>移除事件：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">box.onclick = <span class="literal">null</span>;</span><br></pre></td></tr></table></figure>

<p>on+事件名称注册事件的缺点：</p>
<p>同一个元素同一类型的事件，只能注册一个，如果注册了多个，会出现覆盖问题。</p>
<h2 id="注册事件的新方式"><a href="#注册事件的新方式" class="headerlink" title="注册事件的新方式"></a>注册事件的新方式</h2><p><strong>addEventListener与removeEventListener</strong></p>
<blockquote>
<p>现代浏览器支持的注册事件的新方式，这种方式注册的事件不会出现覆盖问题。</p>
</blockquote>
<p>addEventListener的语法</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//第一个参数：事件的类型：click mouseover</span></span><br><span class="line"><span class="comment">//第二个参数：函数，监听者，每次点击，这个函数就执行。</span></span><br><span class="line"><span class="comment">//第三个参数：是否使用捕获，默认为false，表示冒泡</span></span><br><span class="line">addEventListener(type, func, useCapture);</span><br></pre></td></tr></table></figure>

<p><strong>tips：如果想要让你注册的事件能够移除，不能使用匿名函数。</strong> </p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">fn1</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    alert(<span class="string">"hehe"</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//如果想让注册的事件能移除，不能用匿名函数。</span></span><br><span class="line">box.addEventListener(<span class="string">"click"</span>, fn1, <span class="literal">false</span>);</span><br></pre></td></tr></table></figure>

<p>removeEventListen的语法</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//第一个参数：参数类型</span></span><br><span class="line"><span class="comment">//第二个参数：要移除的那个函数</span></span><br><span class="line"><span class="comment">//第三个参数：false</span></span><br><span class="line">removeEventListener(type, func, useCapture);</span><br></pre></td></tr></table></figure>

<p><strong>attachEvent与detachEvent（了解）</strong></p>
<blockquote>
<p>IE678不支持addEventListener与removeEventListen两个方法，但是支持attachEvent与detachEvnet</p>
</blockquote>
<p>attachEvent的用法：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//type:事件类型   需要加上on   onclick  onmouseenter</span></span><br><span class="line"><span class="comment">//func:需要执行的那个事件</span></span><br><span class="line">attachEvent(type, func)</span><br></pre></td></tr></table></figure>

<p>detachEvent的用法</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//type:事件类型   需要加上on   onclick  onmouseenter</span></span><br><span class="line"><span class="comment">//func:需要执行的那个事件</span></span><br><span class="line">detachEvent(type, func)</span><br></pre></td></tr></table></figure>



<h1 id="事件对象"><a href="#事件对象" class="headerlink" title="事件对象"></a>事件对象</h1><h2 id="事件对象的概述"><a href="#事件对象的概述" class="headerlink" title="事件对象的概述"></a>事件对象的概述</h2><blockquote>
<p>在触发某个事件的时候，都会产生一个事件对象Event，这个对象中包含所有与事件相关的一些信息，包括触发事件的元素，事件的类型以及其他与事件相关的信息。</p>
</blockquote>
<p>鼠标事件触发时，事件对象中会包含鼠标的位置信息。</p>
<p>键盘事件触发时，事件对象中会包含按下的键相关的信息。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">每一个事件在触发时，都会产生一个事件对象。</span><br><span class="line">你见或者不见，我就在那里，不悲不喜。</span><br><span class="line">你爱或者不爱，爱就在那里，不增不减。</span><br><span class="line">你用或者不用，我都会给你，不离不弃。</span><br></pre></td></tr></table></figure>

<h2 id="获取事件对象"><a href="#获取事件对象" class="headerlink" title="获取事件对象"></a>获取事件对象</h2><blockquote>
<p>既然事件对象中存储了这么多的信息，我们首先需要做的就是获取到这个事件对象。</p>
</blockquote>
<p>获取事件对象非常的简单，只需要在注册事件的时候，指定一个形参即可。这个形参就是我们想要获取到的事件对象。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">btn.onclick = <span class="function"><span class="keyword">function</span>(<span class="params">event</span>)</span>&#123;</span><br><span class="line">    <span class="comment">//event就是事件对象，里面包含了事件触发时的一些信息。</span></span><br><span class="line">	<span class="built_in">console</span>.log(event);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="事件对象的常用属性"><a href="#事件对象的常用属性" class="headerlink" title="事件对象的常用属性"></a>事件对象的常用属性</h2><blockquote>
<p>事件对象中有很多很多的属性，但是很多属性并不常用。我们经常用到的是<strong><em>鼠标位置信息</em></strong> 和<strong><em>键盘码</em></strong>  相关的信息。</p>
</blockquote>
<p>记录了鼠标位置信息的相关属性</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">screenX与screenY：光标相对于屏幕左上角的水平位置与垂直位置。</span><br><span class="line">clientX与clientY：光标相对于可视区左上角的水平位置和垂直位置。</span><br><span class="line">pageX与pageY：光标相对于网页（文档<span class="built_in">document</span>）左上角的水平位置与垂直位置（推荐使用）</span><br></pre></td></tr></table></figure>

<p>【案例:让小天使飞.html】</p>
<p>记录了键盘码的属性</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">获取键盘码: 在键盘事件中，通过e.keyCode可以获取到按下的键盘码</span><br></pre></td></tr></table></figure>

<p><img src="/blog/2016/08/07/webAPI/04/keyCode.jpg" alt="键盘码"></p>
<p>【微博评论-添加回车发送功能】</p>
<h2 id="offset系列"><a href="#offset系列" class="headerlink" title="offset系列"></a>offset系列</h2><h3 id="offsetHeight与offsetWidth"><a href="#offsetHeight与offsetWidth" class="headerlink" title="offsetHeight与offsetWidth"></a>offsetHeight与offsetWidth</h3><blockquote>
<p>offsetHeight与offsetWidth</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 获取的是元素真实的高度和宽度</span><br><span class="line"><span class="number">2.</span> 获取到的是数值类型，方便计算</span><br><span class="line"><span class="number">3.</span> offsetHeight与offsetWidth是只读属性，不能设置。</span><br></pre></td></tr></table></figure>

<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">获取宽度和高度offsetWidth与offsetHeight</span><br></pre></td></tr></table></figure>

<h3 id="offsetParent"><a href="#offsetParent" class="headerlink" title="offsetParent"></a>offsetParent</h3><blockquote>
<p>parentNode和offsetParent</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> parentNode始终是父元素</span><br><span class="line"><span class="number">2.</span> offsetParent是离当前元素最近的定位元素(absolute、relative)，如果没有，那就找body</span><br></pre></td></tr></table></figure>

<h3 id="offsetLeft与offsetTop"><a href="#offsetLeft与offsetTop" class="headerlink" title="offsetLeft与offsetTop"></a>offsetLeft与offsetTop</h3><blockquote>
<p>offsetLeft: 自身左侧到offsetParent左侧的距离；</p>
<p>offsetTop:自身顶部到offsetParent顶部的距离；</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>	元素自身与offsetParent真实的距离</span><br><span class="line"><span class="number">2.</span>	获取到的是数值类型，方便计算</span><br><span class="line"><span class="number">3.</span>	只读属性，只能获取，不能设置</span><br></pre></td></tr></table></figure>

<p> <img src="/blog/2016/08/07/webAPI/04/1.png" alt="1"></p>
<h2 id="拖拽特效"><a href="#拖拽特效" class="headerlink" title="拖拽特效"></a>拖拽特效</h2><p>【可拖拽的盒子.html】</p>
<h2 id="放大镜效果（案例）"><a href="#放大镜效果（案例）" class="headerlink" title="放大镜效果（案例）"></a>放大镜效果（案例）</h2><blockquote>
<p>放大镜在开发中是一个很常见的特效，但是所有的放大镜的实现效果都是一样。</p>
</blockquote>
<p>【案例-07-放大镜特效】</p>
<p>实现思路：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 给box注册onmouseover事件，让big和mask显示</span><br><span class="line"><span class="number">2.</span> 给box注册onmouseout事件，让big和mask隐藏</span><br><span class="line"><span class="number">3.</span> 给box注册onmousemove事件，获取鼠标在box中的位置，让mask跟着移动</span><br><span class="line"><span class="number">4.</span> 限定mask的移动范围</span><br><span class="line"><span class="number">5.</span> 根据比例让bigImg跟着移动</span><br></pre></td></tr></table></figure>



<h1 id="事件流"><a href="#事件流" class="headerlink" title="事件流"></a>事件流</h1><h2 id="事件冒泡"><a href="#事件冒泡" class="headerlink" title="事件冒泡"></a>事件冒泡</h2><blockquote>
<p>当一个元素的事件被触发时，同样的事件将会在该元素的所有祖先元素中依次被触发。这一过程被称为事件冒泡。</p>
</blockquote>
<p>说白了就是：当我们触发了子元素的某个事件后，父元素对应的事件也会触发。</p>
<p> <img src="/blog/2016/08/07/webAPI/04/bubble.png" alt="bubble"></p>
<h2 id="事件捕获（了解）"><a href="#事件捕获（了解）" class="headerlink" title="事件捕获（了解）"></a>事件捕获（了解）</h2><blockquote>
<p>事件捕获是火狐浏览器提出来的，IE678不支持事件捕获（基本上，我们都是用事件冒泡）<br>事件的处理将从DOM层次的根开始，而不是从触发事件的目标元素开始，事件被从目标元素的所有祖先元素依次往下传递</p>
</blockquote>
<p> <img src="/blog/2016/08/07/webAPI/04/capture.png" alt="capture"></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//当addEventListener第三个参数为true时，表示事件捕获</span></span><br><span class="line">arr[i].addEventListener(<span class="string">"click"</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="keyword">this</span>);</span><br><span class="line">&#125;,<span class="literal">true</span>);</span><br></pre></td></tr></table></figure>

<h2 id="事件的三个阶段"><a href="#事件的三个阶段" class="headerlink" title="事件的三个阶段"></a>事件的三个阶段</h2><ol>
<li><p>事件的捕获阶段</p>
</li>
<li><p>事件的目标阶段（触发自己的事件）</p>
</li>
<li><p>事件的冒泡阶段</p>
<p><img src="/blog/2016/08/07/webAPI/04/three.png" alt="three"></p>
</li>
</ol>
<p>事件有三个阶段，首先发生的是捕获阶段，然后是目标阶段，最后才是冒泡阶段，让<code>addEventLinstener</code>第三个参数为true时，表示该事件在捕获阶段发生，如果第三个参数为false，表示该事件在冒泡阶段发生。</p>
<h2 id="阻止事件冒泡"><a href="#阻止事件冒泡" class="headerlink" title="阻止事件冒泡"></a>阻止事件冒泡</h2><blockquote>
<p>通过e.stopPropagation()方法可以阻止事件冒泡</p>
</blockquote>
<p>【案例：登录框】</p>
<h2 id="事件补充：阻止浏览器默认行为"><a href="#事件补充：阻止浏览器默认行为" class="headerlink" title="事件补充：阻止浏览器默认行为"></a>事件补充：阻止浏览器默认行为</h2><blockquote>
<p>阻止浏览器默认行为除了使用 return false。</p>
<p>还可以通过 e.preventDefault() 可以阻止浏览器的默认行为</p>
</blockquote>
<h1 id="常见的事件"><a href="#常见的事件" class="headerlink" title="常见的事件"></a>常见的事件</h1><blockquote>
<p>常见的鼠标事件</p>
</blockquote>
<p>onmousedown:鼠标按下事件</p>
<p>onmouseup:鼠标弹起事件</p>
<p>onclick:单击事件</p>
<p>ondblclick：双击事件</p>
<p>onmouseover：鼠标经过事件</p>
<p>onmouseout：鼠标离开事件</p>
<p>onmousemove：鼠标移动事件</p>
<p>onfocus：鼠标获得焦点事件</p>
<p>onblur：鼠标失去焦点事件</p>
<blockquote>
<p>常见的键盘事件</p>
</blockquote>
<p>onkeydown:键盘按下时触发</p>
<p>onkeyup:键盘弹起时触发</p>
<blockquote>
<p>对于鼠标事件，事件对象中有一系列的XY记录了鼠标的位置信息。</p>
<p>键盘事件中，事件对象有一个event.keyCode属性，记录了按下去的键的键盘码</p>
</blockquote>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/blog/tags/dom%E6%93%8D%E4%BD%9C/" rel="tag"># dom操作</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/blog/2016/08/07/webAPI/03/" rel="prev" title="webAPI03">
      <i class="fa fa-chevron-left"></i> webAPI03
    </a></div>
      <div class="post-nav-item">
    <a href="/blog/2016/09/03/webAPI/05/" rel="next" title="webAPI05">
      webAPI05 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#注册事件的两种方式"><span class="nav-text">注册事件的两种方式</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#on-事件名称"><span class="nav-text">on+事件名称</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#注册事件的新方式"><span class="nav-text">注册事件的新方式</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#事件对象"><span class="nav-text">事件对象</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#事件对象的概述"><span class="nav-text">事件对象的概述</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#获取事件对象"><span class="nav-text">获取事件对象</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#事件对象的常用属性"><span class="nav-text">事件对象的常用属性</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#offset系列"><span class="nav-text">offset系列</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#offsetHeight与offsetWidth"><span class="nav-text">offsetHeight与offsetWidth</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#offsetParent"><span class="nav-text">offsetParent</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#offsetLeft与offsetTop"><span class="nav-text">offsetLeft与offsetTop</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#拖拽特效"><span class="nav-text">拖拽特效</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#放大镜效果（案例）"><span class="nav-text">放大镜效果（案例）</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#事件流"><span class="nav-text">事件流</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#事件冒泡"><span class="nav-text">事件冒泡</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#事件捕获（了解）"><span class="nav-text">事件捕获（了解）</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#事件的三个阶段"><span class="nav-text">事件的三个阶段</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#阻止事件冒泡"><span class="nav-text">阻止事件冒泡</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#事件补充：阻止浏览器默认行为"><span class="nav-text">事件补充：阻止浏览器默认行为</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#常见的事件"><span class="nav-text">常见的事件</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="大春哥"
      src="/blog/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">大春哥</p>
  <div class="site-description" itemprop="description">IT</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/blog/archives/">
        
          <span class="site-state-item-count">35</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/blog/categories/">
          
        <span class="site-state-item-count">11</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/blog/tags/">
          
        <span class="site-state-item-count">17</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">大春哥</span>
</div>

        








      </div>
    </footer>
  </div>

  
  <script src="/blog/lib/anime.min.js"></script>
  <script src="/blog/lib/velocity/velocity.min.js"></script>
  <script src="/blog/lib/velocity/velocity.ui.min.js"></script>

<script src="/blog/js/utils.js"></script>

<script src="/blog/js/motion.js"></script>


<script src="/blog/js/schemes/pisces.js"></script>


<script src="/blog/js/next-boot.js"></script>




  




  
<script src="/blog/js/local-search.js"></script>













  

  

</body>
</html>
